package com.doushu.websocket.task;

import com.alibaba.fastjson.JSON;
import com.doushu.websocket.controller.WebSocketServer;
import com.doushu.websocket.dto.ErrorDto;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class PingTask {

    @Scheduled(cron = "0/10 * * * * ?")
    public void ysPing() {
        ErrorDto errorDto = new ErrorDto();
        errorDto.setType("YsPing");
        for (ConcurrentHashMap.Entry<String, WebSocketServer> entry: WebSocketServer.webSocketMap.entrySet()) {
            try {
                entry.getValue().sendMessage(JSON.toJSONString(errorDto));
            } catch (IOException e) {
                System.out.println("消息发送出错");
            }
        }
    }
}
